<?php

namespace cms\modules\v1\models\user;

use common\models\CacheKeys;
use common\models\Common;
use common\models\Functions;
use common\models\Status;
use yii\base\Exception;
use Yii;

/**
 * 用户实名审核
 * Class OperationLog
 * @package cms\modules\v1\models\system
 */
class UserBusiness extends \common\models\user\UserBusiness
{

    /**
     * 返回列表数据
     * @param int $page
     * @param int $size
     * @param array $search
     * @return array
     */
    public function getList($page = 1, $size = 20) {
        try {
            //搜索条件
            $searchWhere  = $this->getSearch();//自定义搜索条件
            //得到文章的总数
            $count = static::getObjectCountByAndWhere($searchWhere['where'], $searchWhere['andWhere'])->count();
            $data = static::setPageinationAPI($page, $size, $count);
            //设置分页
            $offset = ($page - 1) * $size;

            //排序 order
            $orderSql = 'create_time DESC';

            $query = static::getObjectByAndWhere($searchWhere['where'],$searchWhere['andWhere'], "*", $orderSql, $offset, $size);
            $data['data'] = $this->handelList($query);

            return Functions::formatRestAPI(0, '', $data);
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }

    /**
     * 自定义参数的搜索  搜索,搜索也分每一列
     * 这里要根据业务逻辑进行修改
     */
    public function getSearch () {
        $where = [];
        $andWhere = [];
        $status = Yii::$app->request->get('status','');
        $loginName = Yii::$app->request->get('login_name','');
        $user_phone = Yii::$app->request->get('user_phone','');

        if ($user_phone ) {
            $andWhere[] = ['like','user_phone',$user_phone];
        }

        if ($status) {
            $where['status'] = (int) $status;
        }
        if ($loginName) {
            $ids = \common\models\user\User::searchByName($loginName);
            $where['user_id'] = $ids;
        }
        return [
            'where' => $where,
            'andWhere' => $andWhere,
        ];
    }

    /**
     * 处理初始化数据
     * @param $query
     * @return mixed
     */
    public function handelList($query = null) {
        if (empty($query)) {
            return null;
        }
        $list = $query->with([
            'user' => function ($q) {
                $q -> select(['id','login_name','nick_name']);
            },
        ])->asArray()->all();
        foreach($list as &$v){
            $v['bank_image'] = Common::getImages($v['bank_image']);
            $v['business_image'] = Common::getImages($v['business_image']);
            $v['place_image'] = Common::getImages($v['place_image']);
            $v['card_just'] = Common::getImages($v['card_just']);
            $v['card_back'] = Common::getImages($v['card_back']);
        }

        return $list;
    }

    /**
     * 审核
     * @return array
     * @throws \yii\db\Exception
     */

    public function examineInfo() {
        $trans = Yii::$app->db->beginTransaction();
        try{
            $id = Yii::$app->request->post('id','');
            $status = Yii::$app->request->post('status','');
            $reason = Yii::$app->request->post('reason','');
            if (empty($id)|| empty($status)){
                throw new Exception('缺少参数');
            }

            if (!is_array($id)) {
                $ids = [$id];
            } else {
                $ids = $id;
            }

            if($status == Status::COMMON_CHECKING_DEFEAT) {
                if (empty($reason)){
                    throw new Exception('请填写驳回理由');
                }
                $data['reason'] = $reason;
            }

            foreach ($ids as $v) {
                $data['status'] = $status;
                $data['update_time'] = date('Y-m-d H:i:s');
                $res = static::updateData($data, ['id' => $v]);

                if ($res === false){
                    throw new Exception('操作失败');
                }

                $info = static::getOneInfoById($v);

                $updateUser['is_service'] = $status == Status::COMMON_CHECKING_DEFEAT ? Status::COMMON_RADIO_NO : Status::COMMON_RADIO_YES;
                $res = User::updateData($updateUser, ['id' => $info['user_id']]);
                if ($res === false){
                    throw new Exception('操作失败');
                }

            }

            $trans->commit();
            return Functions::formatRestAPI(0, '操作成功');
        } catch (Exception $e) {
            $trans->rollBack();
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }


}
